package cn.codexin.wechatofficial.service;

import cn.codexin.wechatofficial.mapper.HTDB.dbo.RealTimeMapper;
import cn.codexin.wechatofficial.mapper.HTHIS.dbo.RealTimeMapper2;
import cn.codexin.wechatofficial.model.dbo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ly
 * @date 2020/8/14 0014 1:02
 */
@Service
public class RealTimeService {

    @Autowired
    private RealTimeMapper realTimeMapper;

    @Autowired
    private RealTimeMapper2 realTimeMapper2;

    @Autowired
    private TimeService timeService;

    @Autowired
    private MVStatusService mvStatusService;

    //主页面中的遥测信息,动画渲染界面
    public List<MVIndexPojo> MVIndex(int ModuleID){
        List<MVAlarmDef> mvAlarmDefList = MVAlarmList(ModuleID);
        List<MVIndexPojo> mvIndexList = new ArrayList<>();
        for (MVAlarmDef mvAlarmDef : mvAlarmDefList) {
            MVIndexPojo mvIndexPojo = new MVIndexPojo();
            mvIndexPojo.setDotID(mvAlarmDef.getDotID())
                    .setDotName(mvAlarmDef.getDotName())
                    .setDotNumber(mvAlarmDef.getDotNumber())
                    .setUnit(mvAlarmDef.getUnit())
                    .setWarn(mvAlarmDef.isWarn())
                    .setValid(mvAlarmDef.isValid())
                    .setValue(mvAlarmDef.getValue())
                    .setDesc(mvAlarmDef.getDesc());
            mvIndexList.add(mvIndexPojo);
        }
        return mvIndexList;
    }
    //主页面中和实时数据以及设备工况的遥信 信息,动画渲染界面
    public List<SPSIndexPojo> SPSIndex(int ModuleID){
        List<SPSAlarmDef> spsAlarmDefList = SPSAlarmList(ModuleID);
        List<SPSIndexPojo> spsIndexPojoList = new ArrayList<>();
        for (SPSAlarmDef spsAlarmPojo : spsAlarmDefList) {
            SPSIndexPojo spsIndexPojo = new SPSIndexPojo();
            spsIndexPojo.setDotID(spsAlarmPojo.getDotID())
                    .setDotName(spsAlarmPojo.getDotName())
                    .setDotNumber(spsAlarmPojo.getDotNumber())
                    .setOffDiscription(spsAlarmPojo.getOffDiscription())
                    .setOnDiscription(spsAlarmPojo.getOnDiscription())
                    .setStatus(spsAlarmPojo.isStatus())
                    .setWarn(spsAlarmPojo.isWarn());
            spsIndexPojoList.add(spsIndexPojo);
        }
        return spsIndexPojoList;
    }
    //遥信点实时告警
    public List<SPSAlarm> SPSAlarm(int ModuleID){
        List<SPSAlarmDef> spsAlarmDefList = SPSAlarmList(ModuleID);
        List<SPSAlarm> spsAlarmList = new ArrayList<>();
        for (SPSAlarmDef spsAlarmDef : spsAlarmDefList) {
            if (spsAlarmDef.getAlarmPermit() == 1){
                SPSAlarm spsAlarm = new SPSAlarm();
                spsAlarm.setDotID(spsAlarmDef.getDotID())
                        .setDotName(spsAlarmDef.getDotName())
                        .setDotNumber(spsAlarmDef.getDotNumber())
                        .setWarn(spsAlarmDef.isWarn());
                spsAlarmList.add(spsAlarm);
            }
        }
        return spsAlarmList;
    }
    //实时告警中的遥测信息
    public List<MVIndexPojo> MVAlarm(int ModuleID){
        List<MVAlarmDef> mvAlarmDefList = MVAlarmList(ModuleID);
        List<MVIndexPojo> mvAlarmList = new ArrayList<>();
        for (MVAlarmDef mvAlarmDef : mvAlarmDefList) {
            if (mvAlarmDef.getAlarmPermit()>0){
                MVIndexPojo mvIndexPojo = new MVIndexPojo();
                mvIndexPojo.setDotID(mvAlarmDef.getDotID())
                        .setDotName(mvAlarmDef.getDotName())
                        .setDotNumber(mvAlarmDef.getDotNumber())
                        .setUnit(mvAlarmDef.getUnit())
                        .setWarn(mvAlarmDef.isWarn())
                        .setValid(mvAlarmDef.isValid())
                        .setValue(mvAlarmDef.getValue())
                        .setDesc(mvAlarmDef.getDesc());
                mvAlarmList.add(mvIndexPojo);
            }
        }
        return mvAlarmList;
    }
    //遥测数据的实时获取
    public List<MVPojo> MVRealTimeList(int ModuleID) {
        List<MVPojo> mvPojoList = realTimeMapper.allMVDots(ModuleID);
        String table = timeService.currentTime("MVValue",Long.valueOf(String.valueOf(new Date().getTime())));
        //进行拼接
        for (MVPojo mvPojo : mvPojoList) {
            MVValuePojo mvValuePojo = realTimeMapper2.currentMVData(mvPojo.getDotID(), table);
            if (mvValuePojo != null) {
                mvPojo.setValue(mvValuePojo.getValue());
                mvPojo.setStatus(mvStatusService.status(mvPojo.getValuesMap(), mvPojo.getValue()));
            } else {
                mvPojo.setValue(0);
            }
        }
        return mvPojoList;
    }

    //遥信数据的实时获取
    public List<SPSPojo> SPSRealTimeList(int ModuleID) {
            List<SPSPojo> spsPojoList = realTimeMapper.allSPSDots(ModuleID);
            String table = timeService.currentTime("SPSValue",Long.valueOf(String.valueOf(new Date().getTime())));
            //进行拼接
        for (SPSPojo spsPojo : spsPojoList) {
            SPSValuePojo spsValuePojo = realTimeMapper2.currentSPSData(spsPojo.getDotID(), table);
            if (spsValuePojo != null) {
                spsPojo.setStatus(spsValuePojo.isStatus());
            } else {
                spsPojo.setStatus(false);
            }
        }
            return spsPojoList;
    }
    //遥信数据告警状态的判断
    public List<SPSAlarmDef> SPSAlarmList(int ModuleID) {
        List<SPSAlarmDef> spsAlarmDefList = realTimeMapper.allAlarmSPSDots(ModuleID);
        String table = timeService.currentTime("SPSValue",Long.valueOf(String.valueOf(new Date().getTime())));
        //进行拼接
        for (SPSAlarmDef spsAlarmDef : spsAlarmDefList) {
            SPSValuePojo spsValuePojo = realTimeMapper2.currentSPSData(spsAlarmDef.getDotID(), table);
            if (spsValuePojo != null) {
                spsAlarmDef.setStatus(spsValuePojo.isStatus());
                //判断是否告警
                if (spsAlarmDef.getAlarmPermit() == 1) {    //判断是否允许告警
                    //将status转换成int类型
                    int temp = spsAlarmDef.isStatus() ? 1 : 0;
                    if (temp == spsAlarmDef.getAlarmThreshold()) {
                        if (spsAlarmDef.getAlarmCondition() == 0) {
                            spsAlarmDef.setWarn(true);
                        } else {
                            spsAlarmDef.setWarn(false);
                        }
                    } else {
                        if (spsAlarmDef.getAlarmCondition() != 0) {
                            spsAlarmDef.setWarn(true);
                        } else {
                            spsAlarmDef.setWarn(false);
                        }
                    }
                }else {     //不允许告警，就设置为false
                    spsAlarmDef.setWarn(false);
                }
            } else {    //  如果为空对象，则设置为false
                spsAlarmDef.setWarn(false);
            }
        }
        return spsAlarmDefList;
    }
    //遥测数据告警判断
    public List<MVAlarmDef> MVAlarmList(int ModuleID) {
        List<MVAlarmDef> mvAlarmDefList = realTimeMapper.allAlarmMVDots(ModuleID);
        String table = timeService.currentTime("MVValue",Long.valueOf(String.valueOf(new Date().getTime())));
        int permit;
        int a;
        int sign;
        int b;
        //进行拼接
        for (int i = 0; i < mvAlarmDefList.size(); i++) {
            MVValuePojo mvValuePojo = realTimeMapper2.currentMVData(mvAlarmDefList.get(i).getDotID(), table);
            if (mvValuePojo != null) {
                mvAlarmDefList.get(i).setValue(mvValuePojo.getValue());
                if (mvAlarmDefList.get(i).getInvalidValue() == mvAlarmDefList.get(i).getValue()){
                    mvAlarmDefList.get(i).setValid(false);
                }else {
                    mvAlarmDefList.get(i).setValid(true);
                }
                //判断是否告警
                permit = mvAlarmDefList.get(i).getAlarmPermit();
                //将permit转化为二进制
                sign = 0;
                b = 0;
                while (permit != 0) {
                    a = permit % 2;
                    permit = permit / 2;
                    sign += a * (Math.pow(10, b));
                    b++;
                }
                //排除无效值
                sign = sign % 10000;
                if (sign / 1000 > 0 && mvAlarmDefList.get(i).getValue() < mvAlarmDefList.get(i).getLLLimit()) {
                    mvAlarmDefList.get(i).setWarn(true);
                    mvAlarmDefList.get(i).setDesc(mvAlarmDefList.get(i).getLLAlarmDesc());
                } else if (sign % 1000 / 100 > 0 && mvAlarmDefList.get(i).getValue() < mvAlarmDefList.get(i).getLLimit()) {
                    mvAlarmDefList.get(i).setWarn(true);
                    mvAlarmDefList.get(i).setDesc(mvAlarmDefList.get(i).getLAlarmDesc());
                } else if (sign % 100 / 10 > 0 && mvAlarmDefList.get(i).getValue() > mvAlarmDefList.get(i).getHLimit()) {
                    if (sign % 10 > 0 && mvAlarmDefList.get(i).getValue() > mvAlarmDefList.get(i).getHHLimit()) {
                        mvAlarmDefList.get(i).setWarn(true);
                        mvAlarmDefList.get(i).setDesc(mvAlarmDefList.get(i).getHHAlarmDesc());
                    } else {
                        mvAlarmDefList.get(i).setWarn(true);
                        mvAlarmDefList.get(i).setDesc(mvAlarmDefList.get(i).getHAlarmDesc());
                    }
                } else {
                    mvAlarmDefList.get(i).setWarn(false);
                }
            }else {
                mvAlarmDefList.get(i).setWarn(false);
            }
        }
        return mvAlarmDefList;
    }
}
